package com.boomsecret.juc.atomic;

import java.util.concurrent.atomic.AtomicIntegerArray;

/**
 * @author wangjinliang on 2019/1/17.
 */
public class AtomicIntegerArrayDemo {
    public static void main(String[] args) {
        AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(10);
        System.out.println(atomicIntegerArray.length());

        System.out.println("--------------");
        atomicIntegerArray.set(0, 20);
        System.out.println(atomicIntegerArray.get(0));

        System.out.println("--------------");
        atomicIntegerArray.lazySet(1, 30);
        System.out.println(atomicIntegerArray.get(1));

        System.out.println("--------------");
        atomicIntegerArray.getAndSet(2, 40);
        System.out.println(atomicIntegerArray.get(2));

        System.out.println("--------------");
        System.out.println(atomicIntegerArray.compareAndSet(2, 40, 41));
        System.out.println(atomicIntegerArray.get(2));

        System.out.println("--------------");
        System.out.println(atomicIntegerArray.compareAndSet(2, 40, 42));
        System.out.println(atomicIntegerArray.get(2));

        System.out.println("--------------");
        System.out.println(atomicIntegerArray.weakCompareAndSet(3, 3, 5));
        System.out.println(atomicIntegerArray.get(3));

        System.out.println("--------------");
        System.out.println(atomicIntegerArray.getAndIncrement(4));
        System.out.println(atomicIntegerArray.get(4));

        System.out.println("--------------");
        System.out.println(atomicIntegerArray.getAndDecrement(5));
        System.out.println(atomicIntegerArray.get(5));

        System.out.println("--------------");
        System.out.println(atomicIntegerArray.getAndAdd(6, 10));
        System.out.println(atomicIntegerArray.get(6));

        System.out.println("--------------");
        System.out.println(atomicIntegerArray.incrementAndGet(7));
        System.out.println(atomicIntegerArray.get(7));

        System.out.println("--------------");
        System.out.println(atomicIntegerArray.decrementAndGet(7));
        System.out.println(atomicIntegerArray.get(7));

        System.out.println("--------------");
        System.out.println(atomicIntegerArray.addAndGet(7, 10));
        System.out.println(atomicIntegerArray.get(7));

        System.out.println("--------------");
        int result = atomicIntegerArray.getAndUpdate(8, operand -> operand + 2);
        System.out.println(result);
        System.out.println(atomicIntegerArray.get(8));

        System.out.println("--------------");
        int result1 = atomicIntegerArray.updateAndGet(8, operand -> operand + 8);
        System.out.println(result1);
        System.out.println(atomicIntegerArray.get(8));

        System.out.println("--------------");
        int result2 = atomicIntegerArray.getAndAccumulate(9, 2, (left, right) -> right + left);
        System.out.println(result2);
        System.out.println(atomicIntegerArray.get(9));

        System.out.println("--------------");
        int result3 = atomicIntegerArray.accumulateAndGet(9, 8, (left, right) -> right + left);
        System.out.println(result3);
        System.out.println(atomicIntegerArray.get(9));
    }
}
